package com.mnemonic.views.field;

import com.mnemonic.lifecycle.Lifecycle;
import com.mnemonic.utils.DateUtils;

/**
 * 计时器
 * 
 * @author lifeng
 * @date 2021/05/13
 */
public class ClockField extends Field implements Lifecycle, Runnable {

    private static final long serialVersionUID = 1L;

    /**
     * 显示名称
     */
    private String name;

    /**
     * 当前的点数
     */
    private volatile Integer times = 0;

    /**
     * 简单的计数器
     */
    private Thread timer;

    /**
     * 倒计时模式
     */
    private boolean countdown = false;

    /**
     * 最大值
     */
    private volatile Integer max = 0;

    /**
     * 标识是否运行状态
     */
    private volatile boolean running = false;

    /**
     * 标识是否运行状态 -- countdown 继续的标志
     */
    private volatile boolean countdownRunning = false;

    /**
     * 监听
     */
    private CountdownListener listener;

    /**
     * 普通的计时器
     * 
     * @param name
     */
    public ClockField(String name) {
        super(name);
        this.name = name;
        this.timer = new Thread(this);
    }

    /**
     * count down 计时器
     * 
     * @param name
     * @param max
     * @param listener
     */
    public ClockField(String name, int max, CountdownListener listener) {
        this(name);
        this.countdown = true;
        this.max = max;
        this.listener = listener;
    }

    @Override
    public void run() {
        while (running) {
            if (this.countdown) {
                // countdown 忽略此次执行
                if (this.countdownRunning) {
                    // 常规的countdown 操作
                    times--;
                    if (times <= 0) {
                        countdownRunning = false;
                        // 发送一次coundown 事件， 需要执行 reset 之后才继续执行
                        if (this.listener != null) {
                            this.listener.countdown(this);
                        }
                    }
                }
            } else {
                times++;
            }
            if (times < 0) {
                this.setText(this.name + "不限制");
            } else if (times == 0) {
                this.setText(this.name + "0秒");
            } else {
                this.setText(this.name + DateUtils.formatLongTime(times * 1000, null));
            }

            // 每1秒运行一次
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    }

    @Override
    public void start() {
        running = true;
        this.timer.start();
    }

    @Override
    public void init() {

    }

    @Override
    public void destory() {
        running = false;
    }

    /**
     * 重置
     */
    public void reset() {
        if (this.max > 0) {
            this.times = this.max;
            this.countdownRunning = true;
        }
    }

    /**
     * countdown 的回调
     * 
     * @author lifeng
     * @date 2021/05/13
     */
    public static interface CountdownListener {

        /**
         * countdown 事件
         * 
         * @param clock
         */
        void countdown(ClockField clock);
    }
}
